const Hejia = (window as any).Hejia;
import { deviceId } from './staticData';

import useStore from '../store/index';
export const getDeviceInfo = () => {
  // deviceId
  Hejia.getDeviceInfo(
    {},
    function (obj: any) {
      console.log(obj, '走 store 快照接口设备参数---');
      // if (obj.device == undefined) return;

      /**
       * 第一级别：离线，设备离线级别最高，如果设备是离线状态，需要展示离线页面
       * 第二级别：故障
       */
      let result = { isOnline: obj.device.connected };
      const { setInfo, info } = useStore.getState();

      result = { ...info, ...result };
      setInfo(result);

      parseData(obj.device.parameters);
    },
    function (msg: any, obj: any) {
      console.log(obj, msg);
    }
  );
};

const parseData = (params: any) => {
  const result: any = {};
  const { setInfo, info } = useStore.getState();

  params.forEach((e: any) => {
    const keys = [
      'switch',
      'mode',
      'booking',
      'remainder',
      'Duration',
      'state',
      'fault',
    ];

    if (keys.includes(e.name)) {
      result[e.name] = Number(e.value);

      setInfo({ ...info, ...result });
    }
  });

  console.log(info, 'info1111');
};
